<?PHP // functions.php

/////////////////////////////////////////////////////////////////////////
// mserv functions, adapted from mp3kd by Kai Duebbert <kai@duebbert.de>
// from mserv-php
/////////////////////////////////////////////////////////////////////////

function unqueue( $album, $track ) {
	$result_array = mserv( "unqueue $album $track" );
	$status_code = $result_array[0];
	switch( $status_code ) {
		/* Success */
		case "254":
			$i = 0;
			$result = array(
				"MESSAGE" => $result_array[++$i],
				"ALBUM_NO" => $result_array[++$i],
				"TRACK_NO" => $result_array[++$i],
				"ARTIST" => $result_array[++$i],
				"TITLE" => $result_array[++$i]
			);
			break;
		/* Error message */
		case "409":
		case "503":
			$result = array(
				"MESSAGE" => $result_array[1],
			);
			break;
		default:
			error_log("===[ERROR]===: Don't know what to do!? Function unqueue: " . $result_array[count($result_array)-1]);
			break;
	}
	$result["STATUS"] = $status_code;
	$result["COMPLETE_MESSAGE"] = $result_array[count($result_array)-1];
	return $result;
}


function queue( $album="", $track="" ) {
	$result_array = mserv( "queue $album $track" );
	$status_code = $result_array[0];
	switch( $status_code ) {
		/* Success */
		case "247":
			$result = array(
				"MESSAGE" => $result_array[1],
			);
			$i = 2;
			while ( $i < count( $result_array )-1 ) {
				$result[] = array(
					"USER" => $result_array[$i++],
					"ALBUM_NO" => $result_array[$i++],
					"TRACK_NO" => $result_array[$i++],
					"ARTIST" => $result_array[$i++],
					"TITLE" => $result_array[$i++],
					"RATING" => $result_array[$i++]
				);
			}
			break;
		/* Can't add to queue */
		case "510":
		/* No files in queue */
		case "404":
			$result = array(
				"MESSAGE" => $result_array[1],
			);
			break;
		/* List queue */
		case "225":
			$result = array(
				"MESSAGE" => $result_array[1],
			);
			$i = 2;
            $queuecount=0;
			while ( $i < count( $result_array )-1 ) {
				$result[$queuecount] = array(
					"USER" => $result_array[$i++],
					"ALBUM_NO" => $result_array[$i++],
					"TRACK_NO" => $result_array[$i++],
					"ARTIST" => $result_array[$i++],
					"TITLE" => $result_array[$i++],
					"RATING" => $result_array[$i++],
					"LENGTH" => $result_array[$i++]
				);
                $queuecount++;
			}
            $result["COUNT"] = $queuecount;
			break;
		default:
			error_log("===[ERROR]===: Don't know what to do!? Function queue: " . $result_array[count($result_array)-1]);
			break;
	}
	$result["STATUS"] = $status_code;
	$result["COMPLETE_MESSAGE"] = $result_array[count($result_array)-1];
	return $result;
}

function top() {
	$result_array = mserv( "top" );
	$status_code = $result_array[0];

	switch( $status_code ) { 	
	/* Success */
		case "234":
			$result = array(
				"MESSAGE" => $result_array[1],
			);
			$i = 2;
            $topcount=0;
			while ( $i < count( $result_array )-1 ) {
				$result[$topcount] = array(
					"PROB" => $result_array[$i++],
					"ALBUM_NO" => $result_array[$i++],
					"TRACK_NO" => $result_array[$i++],
					"ARTIST" => $result_array[$i++],
					"TITLE" => $result_array[$i++],
					"RATING" => $result_array[$i++],
					"LENGTH" => $result_array[$i++]
				);
                $topcount++;
			}
            $result["COUNT"] = $topcount;
			break;
		default:
			error_log("===[ERROR]===: Don't know what to do!? Function queue: " . $result_array[count($result_array)-1]);
			break;
	}
	$result["STATUS"] = $status_code;
	$result["COMPLETE_MESSAGE"] = $result_array[count($result_array)-1];
	return $result;
}

function history() {
	$result_array = mserv( "history" );
	$status_code = $result_array[0];
	switch( $status_code ) {
		/* Success */
		case "241":
			$result = array(
				"MESSAGE" => $result_array[1],
			);
			$i = 2;
			$historycount=0;
			while ( $i < count( $result_array )-1 ) {
				$result[$historycount] = array(
					"USER" => $result_array[$i++],
					"ALBUM_NO" => $result_array[$i++],
					"TRACK_NO" => $result_array[$i++],
					"ARTIST" => $result_array[$i++],
					"TITLE" => $result_array[$i++],
					"RATING" => $result_array[$i++]
				);
				$historycount++;
			}
			$result["COUNT"] = $historycount;
			break;
		default:
			error_log("===[ERROR]===: Don't know what to do!? Function queue: " . $result_array[count($result_array)-1]);
			break;
	}
	$result["STATUS"] = $status_code;
	$result["COMPLETE_MESSAGE"] = $result_array[count($result_array)-1];
	return $result;
}


function search( $query ) {
error_log("===[DEBUG]===: $query");
	$result_array = mserv( "search $query" );
	$status_code = $result_array[0];
	switch( $status_code ) {
		/* Success */
		case "243":
			$result = array(
				"MESSAGE" => $result_array[1],
			);
			$i = 2;
            $searchcount=0;
			while ( $i < count( $result_array )-1 ) {
				$result[$searchcount] = array(
					"ALBUM_NO" => $result_array[$i++],
					"TRACK_NO" => $result_array[$i++],
					"ARTIST" => $result_array[$i++],
					"TITLE" => $result_array[$i++],
					"RATING" => $result_array[$i++],
					"LENGTH" => $result_array[$i++]
				);
                $searchcount++;
			}
			break;
		/* Nothing found. */
		case "520":
			$result = array(
				"MESSAGE" => $result_array[1]
			);
			break;
		default:
			error_log("===[ERROR]===: Don't know what to do!? Function search: " . $result_array[count($result_array)-1]);
			break;
	}
	$result["COUNT"] = $searchcount;
	$result["STATUS"] = $status_code;
	$result["COMPLETE_MESSAGE"] = $result_array[count($result_array)-1];
	return $result;
}

function prev_track() {
	$result_array = history();
	if($result_array["COUNT"] > 1) {
		$status_code = "240";
		$result = array(
			"MESSAGE" => $result_array[1]["MESSAGE"],
			"ALBUM_NO" => $result_array[1]["ALBUM_NO"],
			"TRACK_NO" => $result_array[1]["TRACK_NO"],
			"ARTIST" => $result_array[1]["ARTIST"],
			"TITLE" => $result_array[1]["TITLE"],
		);
		queue($result["ALBUM_NO"], $result["TRACK_NO"]);
		next_track();
	} else {
		$status_code = "403";
	}
	$result["STATUS"] = $status_code;
}

function next_track() {
	$result_array = mserv( "next" );
	$status_code = $result_array[0];
	switch( $status_code ) {
		/* Success */
		case "240":
			$result = array(
				"MESSAGE" => $result_array[1],
				"ALBUM_NO" => $result_array[2],
				"TRACK_NO" => $result_array[3],
				"ARTIST" => $result_array[4],
				"TITLE" => $result_array[5]
			);
			break;
		/* No more songs to be played. */
		case "403":
			global $playing;
			$playing = false;
			$result = array(
				"MESSAGE" => $result_array[1]
			);
			break;
		default:
			error_log("===[ERROR]===: Don't know what to do!? Function next_track: " . $result_array[count($result_array)-1]);
			break;
	}
	$result["STATUS"] = $status_code;
	$result["COMPLETE_MESSAGE"] = $result_array[count($result_array)-1];
	return $result;
}


function status() {
	$result_array = mserv( "status" );
	$status_code = $result_array[0];
	switch( $status_code ) {
		/* Playing a song */
		case "222":
			$result = array(
				"FILTER" => $result_array[2],
				"TRACK_INCLUDE" => $result_array[3],
				"TRACK_EXCLUDE" => $result_array[4],
				"ALBUM_NO" => $result_array[5],
				"TRACK_NO" => $result_array[6],
				"ARTIST" => $result_array[7],
				"TITLE" => $result_array[8],
				"DATE" => $result_array[9],
				"CURRENT_POS" => $result_array[10],
				"RANDOM" => $result_array[11],
				"RANDOM_WEIGHT" => $result_array[12]
			);
			global $playing;
			$playing = true;
			break;
		/* Pausing a song */
		case "223":
			$result = array(
				"FILTER" => $result_array[2],
				"TRACK_INCLUDE" => $result_array[3],
				"TRACK_EXCLUDE" => $result_array[4],
				"ALBUM_NO" => $result_array[5],
				"TRACK_NO" => $result_array[6],
				"ARTIST" => $result_array[7],
				"TITLE" => $result_array[8],
				"DATE" => $result_array[9],
				"CURRENT_POS" => $result_array[10],
				"RANDOM" => $result_array[11],
				"RANDOM_WEIGHT" => $result_array[12]
			);
			global $pausing;
			$pausing = true;
			break;
		/* Nothing playing */
		case "224":
			$result = array(
				"FILTER" => $result_array[2],
				"TRACK_INCLUDE" => $result_array[3],
				"TRACK_EXCLUDE" => $result_array[4],
				"RANDOM" => $result_array[5],
				"RANDOM_WEIGHT" => $result_array[6]
			);
			break;
		default:
			error_log("===[ERROR]===: Don't know what to do!? Function status: " . $result_array[count($result_array)-1]);
			break;
	}
	$result["STATUS"] = $status_code;
	$result["COMPLETE_MESSAGE"] = $result_array[count($result_array)-1];
	return $result;
}


function filter($filter) {  // Sets filter pattern
	$result_array = mserv( "filter $filter" );
	$status_code = $result_array[0];
	switch( $status_code ) {
		/* Filter set OK */
		case "269":
			$result = array(
				"TRACK_INCLUDE" => $result_array[2],
				"TRACK_EXCLUDE" => $result_array[3]
			);
			break;
		default:
			error_log("===[ERROR]===: Don't know what to do!? Function ratings: " . $result_array[count($result_array)-1]);
			break;
	}
	$result["STATUS"] = $status_code;
	$result["COMPLETE_MESSAGE"] = $result_array[count($result_array)-1];
	return $result;
}

function ratings($album="", $track="") {
	$result_array = mserv( "ratings $album $track" );
	$status_code = $result_array[0];
	switch( $status_code ) {
		/* Rating of a song */
		case "229":
			$result = array(
				"ALBUM_NO" => $result_array[2],
				"TRACK_NO" => $result_array[3],
				"ARTIST" => $result_array[4],
				"TITLE" => $result_array[5]
			);


			$i = 6;
			$ratingscount=0;
			while ( $i < count( $result_array )-1 ) {
				$result[$ratingscount] = array(
					"USER" => $result_array[$i++],
					"RATING" => $result_array[$i++],
					"TIME" => $result_array[$i++]
					);
				$ratingscount++;
			}

			$result["COUNT"] = $ratingscount;
			break;
		default:
			error_log("===[ERROR]===: Don't know what to do!? Function ratings: " . $result_array[count($result_array)-1]);
			break;
	}
	$result["STATUS"] = $status_code;
	$result["COMPLETE_MESSAGE"] = $result_array[count($result_array)-1];
	return $result;
}


function info($album="", $track="") {  // Get information about the song playing.
	$result_array = mserv( "info $album $track" );
	$status_code = $result_array[0];
	switch( $status_code ) {
		/* Info of a song */
		case "246":
			$result = array(
				"ALBUM_NO" => $result_array[2],
				"TRACK_NO"  => $result_array[3],
				"ALBUM_ARTIST" => $result_array[4],
				"ALBUM" => $result_array[5],
				"ARTIST" => $result_array[6],
				"TITLE" => $result_array[7],
				"YEAR" => $result_array[8],
				"UNKNOWN3" => $result_array[9], // TODO
				"LAST_PLAYED" => $result_array[10],
				"UNKNOWN1" => $result_array[11], // TODO
				"UNKNOWN2" => $result_array[12], //TODO
				"RATING" => $result_array[13],
				"GENRE" => $result_array[14],
				"FILTER" => $result_array[15],
				"LENGTH" => $result_array[16],
				"COMMENT" => $result_array[17]
			);
			break;
		/* Info of an album */
		case "245":
			$result = array(
				"ALBUM_NO" => $result_array[2],
				"ALBUM_ARTIST" => $result_array[3],
				"ALBUM" => $result_array[4]
			);
			break;
		default:
			error_log("===[ERROR]===: Don't know what to do!? Function info: " . $result_array[count($result_array)-1]);
			break;
	}
	$result["STATUS"] = $status_code;
	$result["COMPLETE_MESSAGE"] = $result_array[count($result_array)-1];
	return $result;
}


function albums() {  // Return a list of all albums
	$result_array = mserv( "albums" );
	$status_code = $result_array[0];
	switch( $status_code ) {
		case "227":
            $count = count($result_array);
            $i=2;
            $albumcount = 0;
            while ($i < $count) {
                $albumnum  = $result_array[$i];
		$artist = $result_array[$i+1];
                $albumname = $result_array[$i+2];
                $result["$albumnum"] = array($artist, $albumname);
                $albumcount++;
                $i = $i + 3;
            }
			break;
		default:
			error_log("===[ERROR]===: Don't know what to do!? Function info: " . $result_array[count($result_array)-1]);
			break;
	}
	$result["COUNT"] = $albumcount;
	$result["STATUS"] = $status_code;
	$result["COMPLETE_MESSAGE"] = $result_array[count($result_array)-1];
	return $result;
}

function tracks($album) {  // Return a list of all tracks on an album
	$result_array = mserv("tracks $album");
	$status_code = $result_array[0];
	switch( $status_code ) {
		case "228":
            $count = count($result_array) - 1;
	    $result["ARTISTALBUM"] = $result_array[3];
            $result["ALBUMNAME"] = $result_array[4];
            $i=5;
            $trackcount = 0;
            while ($i < $count) {
                $track["ALBUM_NO"] = $result_array[$i];
                $track["TRACK_NO"] = $result_array[$i+1];
                $track["ARTIST"] = $result_array[$i+2];
                $track["TITLE"] = $result_array[$i+3];
                $track["RATING"] = $result_array[$i+4];
                $track["TIME"] = $result_array[$i+5];
                $result["$trackcount"] = $track;
                $trackcount++;
                $i = $i + 6;
            }
			break;
		default:
			error_log("===[ERROR]===: Don't know what to do!? Function info: " . $result_array[count($result_array)-1]);
			break;
	}
	$result["COUNT"] = $trackcount;
	$result["STATUS"] = $status_code;
	$result["COMPLETE_MESSAGE"] = $result_array[count($result_array)-1];
	return $result;
}


function play() {	
	$result_array = mserv( "play" );
	$status_code = $result_array[0];
	switch( $status_code ) {
		/* Play a song */
		case "230":
		/* Already playing */
		case "402":
			$result = array(
				"ALBUM_NO" => $result_array[2],
				"TRACK_NO" => $result_array[3],
				"ARTIST" => $result_array[4],
				"TITLE" => $result_array[5]
			);
			break;
		default:
			error_log("===[ERROR]===: Don't know what to do!? Function play(): " . $result_array[count($result_array)-1]);
			break;
	}
	$result["STATUS"] = $status_code;
	$result["COMPLETE_MESSAGE"] = $result_array[count($result_array)-1];
	return $result;
}


function volume($volume="") {
	$result_array = mserv( "volume $volume" );
	$status_code = $result_array[0];
	switch( $status_code ) {
		case "235":   // print volume
			$result["VOLUME"] = $result_array[2];
			break;
		case "255":   // set volume
			$result["VOLUME"] = $result_array[2];
            break;
		default:
			error_log("===[ERROR]===: Don't know what to do!? Function play(): " . $result_array[count($result_array)-1]);
			break;
	}
	$result["STATUS"] = $status_code;
	$result["COMPLETE_MESSAGE"] = $result_array[count($result_array)-1];
	return $result;
}


function bass($bass="") {	
	$result_array = mserv( "bass $bass" );
	$status_code = $result_array[0];
	switch( $status_code ) {
		case "235":   // print volume
			$result["BASS"] = $result_array[2];
			break;
		case "255":   // set volume
			$result["BASS"] = $result_array[2];
            break;
		default:
			error_log("===[ERROR]===: Don't know what to do!? Function play(): " . $result_array[count($result_array)-1]);
			break;
	}
	$result["STATUS"] = $status_code;
	$result["COMPLETE_MESSAGE"] = $result_array[count($result_array)-1];
	return $result;
}


function treble($treble="") {	
	$result_array = mserv( "treble $treble" );
	$status_code = $result_array[0];
	switch( $status_code ) {
		case "235":   // print volume
			$result["TREBLE"] = $result_array[2];
			break;
		case "255":   // set volume
			$result["TREBLE"] = $result_array[2];
            break;
		default:
			error_log("===[ERROR]===: Don't know what to do!? Function play(): " . $result_array[count($result_array)-1]);
			break;
	}
	$result["STATUS"] = $status_code;
	$result["COMPLETE_MESSAGE"] = $result_array[count($result_array)-1];
	return $result;
}


function rate( $album, $track, $rate ) {
	$result_array = mserv( "rate $album $track $rate" );
	$status_code = $result_array[0];
	switch( $status_code ) {
		case "270":
			$result = array(
				"USER" => $result_array[2],
				"ALBUM_NO" => $result_array[3],
				"TRACK_NO" => $result_array[4],
				"ARTIST" => $result_array[5],
				"TITLE" => $result_array[6],
				"RATING" => $result_array[7]
			);
			break;
		default:
			error_log("===[ERROR]===: Don't know what to do!? Function rate(): " . $result_array[count($result_array)-1]);
			break;
	}
	$result["STATUS"] = $status_code;
	$result["COMPLETE_MESSAGE"] = $result_array[count($result_array)-1];
	return $result;
}


function stop() {
	$result_array = mserv( "stop" );
	$status_code = $result_array[0];
	switch( $status_code ) {
		/* Stop playing */
		case "231":
			$result = array(
				"ALBUM_NO" => $result_array[2],
				"TRACK_NO" => $result_array[3],
				"ARTIST" => $result_array[4],
				"TITLE" => $result_array[5]
			);
			break;
		/* Stop was pressed, while nothing playing. */
		case "401":
			$result = status();
			break;
		default:
			error_log("===[ERROR]===: Don't know what to do!? Function stop(): " . $result_array[count($result_array)-1]);
			break;
	}
	$result["STATUS"] = $status_code;
	$result["COMPLETE_MESSAGE"] = $result_array[count($result_array)-1];
	return $result;
}


function pause() {
	$result_array = mserv( "pause" );
	$status_code = $result_array[0];
	switch( $status_code ) {
		/* Pause playing */
		case "232":
		/* Already paused. */
		case "406":
			global $pausing;
			$result = status();
			$pausing = true;
			break;
		default:
			error_log("===[ERROR]===: Don't know what to do!? Function pause(): " . $result_array[count($result_array)-1]);
			break;
	}
	$result["STATUS"] = $status_code;
	$result["COMPLETE_MESSAGE"] = $result_array[count($result_array)-1];
	return $result;
}


function who() {
	$result_array = mserv( "who" );
	$status_code = $result_array[0];
	switch( $status_code ) {
		/* Ok */
		case "226":
			$result = array(
				"MESSAGE" => $result_array[1],
			);
			$i = 2;
			$whocount=0;
			while ( $i < count( $result_array )-1 ) {
				$result[$whocount] = array(
					"USER" => $result_array[$i++],
					"LEVEL" => $result_array[$i++],
					"IDLE" => $result_array[$i++],
					"FROM" => $result_array[$i++]
				);
			$whocount++;
			}
			$result["COUNT"] = $whocount;
			
		default:
			error_log("===[ERROR]===: Don't know what to do!? Function who(): ". $result_array[count($result_array)-1]);
                        break;
	}
	$result["STATUS"] = $status_code;
	return $result;

}


function add_genre( $new, $album, $track )
{
	$info = info();
	$old = $info["GENRE"];
	$result_array = mserv( "set genre $album $track $old,$new" );
	$status_code = $result_array[0];
	switch( $status_code ) {
		/* Already paused. */
		case "263":
			$result = status();
			break;
		default:
			error_log("===[ERROR]===: Don't know what to do!? Function add_genre(): " . $result_array[count($result_array)-1]);
			break;
	}
	$result["STATUS"] = $status_code;
	$result["COMPLETE_MESSAGE"] = $result_array[count($result_array)-1];
	return $result;
}


function del_genre( $del, $album, $track )
{
	$info = info();
	$old = $info["GENRE"] . ",";
	$genres = ereg_replace( "$del,", "", $old );
	$genres = ereg_replace( ",$", "", $genres );
	$result_array = mserv( "set genre $album $track $genres" );
	$status_code = $result_array[0];
	switch( $status_code ) {
		/* Already paused. */
		case "263":
			$result = status();
			break;
		default:
			error_log("===[ERROR]===: Don't know what to do!? Function add_genre(): " . $result_array[count($result_array)-1]);
			break;
	}
	$result["STATUS"] = $status_code;
	$result["COMPLETE_MESSAGE"] = $result_array[count($result_array)-1];
	return $result;
}


function get_genres() {  // Get all of our genres (playlists!)
	global $playlist_prefix;
	$result_array = mserv( "x genres" );
	return $result_array;
}


function mserv( $cmd ) {
// Executes mservcmd and returns the result in an array.
// Last field of array is the unedited result message.
	global $MSERVCMD;

	$cmd = escapeshellarg( $cmd );
	$result = `$MSERVCMD $cmd`;
	$result_array = preg_split( "/[\t\n]/", trim( $result ) );
	$result_array[count($result_array)] = $result;
		
	for($i=0; $i<sizeof($result_array); $i++) $result_array[$i] = utf8_encode($result_array[$i]);

	return $result_array;
}

